package cn.kimming.service.impl;

import cn.kimming.common.constant.SystemConstant;
import cn.kimming.common.exception.SystemException;
import cn.kimming.common.pojo.Room;
import cn.kimming.common.pojo.RoomType;
import cn.kimming.common.vo.RoomInFoVO;
import cn.kimming.common.vo.RoomVO;
import cn.kimming.mapper.RoomMapper;
import cn.kimming.mapper.RoomTypeMapper;
import cn.kimming.service.IRoomService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 刘铭轩
 * date 2021-05-23
 */
@Service
@Transactional
public class RoomServiceImpl implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RoomTypeMapper roomTypeMapper;

    @Override
    public RoomVO findById(Long id) {
        return roomMapper.findById(id);
    }

    @Override
    public void save(Room bo) {
        checkRoomType(bo.getRoomTypeId());
        checkRoomNum(bo);
        bo.setLiveStatus(SystemConstant.ROOM_LIVE_STATUS_NONE);
        bo.setCleanStatus(SystemConstant.ROOM_CLEAN_STATUS_UNCLEAN);
        roomMapper.insertSelective(bo);
    }

    @Override
    public void update(Room bo) {
        checkRoomType(bo.getRoomTypeId());
        checkRoomNum(bo);
        roomMapper.updateByPrimaryKeySelective(bo);
    }

    @Override
    public void deleteById(Long id) {
        roomMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<JSONObject> find4BuildingCodeFilter() {
        return roomMapper.find4BuildingCodeFilter();
    }

    @Override
    public PageInfo<RoomVO> findAllByPage(int page, Integer roomNum) {
        PageHelper.startPage(page, 10);
        List<RoomVO> list = roomMapper.findAll(roomNum);
        PageInfo<RoomVO> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public PageInfo<RoomInFoVO> findInfo(int page, Integer roomNum) { PageHelper.startPage(page, 10);
        List<RoomInFoVO> list = roomMapper.findInfo(roomNum);
        PageInfo<RoomInFoVO> info = new PageInfo<>(list);
        return info;

    }

    /**
     * 检查是否有对应的房型
     * @param roomTypeId
     */
    private void checkRoomType(Long roomTypeId) {
        if (roomTypeId != null && roomTypeId > 0) {
            RoomType roomType = roomTypeMapper.selectByPrimaryKey(roomTypeId);
            if (roomType == null) throw new SystemException("没有此房型信息");
        }
    }

    /**
     * 检查房间编号是否重复
     * @param bo
     */
    private void checkRoomNum(Room bo) {
        if (bo.getRoomNum() != null) {
            Long idInDb = roomMapper.findIdByRoomNum(bo.getRoomNum());
            if (idInDb != null && !idInDb.equals(bo.getId())) { // 当存在对应房间编号的房间且不是bo对应房间时, 编号重复
                throw new SystemException("该房间编号已存在");
            }
        }
    }

}
